//
//  RegistViewController.m
//  HelpThePoor
//
//  Created by van7ish on 2016/11/29.
//  Copyright © 2016年 van7ish. All rights reserved.
//

#import "RegistViewController.h"
#import "AppDelegate.h"

@interface RegistViewController () <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate>

/** 头视图 */
@property (nonatomic, strong) UIImageView * headerImageView;
/** 注册按钮 */
@property (nonatomic, strong) UIButton * registButton;
/** 手机图标 */
@property (nonatomic, strong) UIImageView * phoneImageView;
/** 验证码图标 */
@property (nonatomic, strong) UIImageView * idCodeImageView;
/** 密码图标 */
@property (nonatomic, strong) UIImageView * pwdImageView;
/** 手机号输入框 */
@property (nonatomic, strong) UITextField * phoneTextField;
/** 验证码入框 */
@property (nonatomic, strong) UITextField * idCodeTextField;
/** 密码输入框 */
@property (nonatomic, strong) UITextField * pwdTextField;
/** 获取验证码按钮 */
@property (nonatomic, strong) UIButton * idCodeButton;
/** 密码可见按钮 */
@property (nonatomic, strong) UIButton * pwdVisibleButton;
/** 查看用户协议按钮 */
@property (nonatomic, strong) UIButton * licenceButton;
/** 用户协议对勾按钮 */
@property (nonatomic, strong) UIButton * correctButton;
/** 倒计时timer */
@property (nonatomic, strong) NSTimer * timer;
/** 倒计时60s */
@property (nonatomic, assign) NSInteger seconds;
/** 定位图标 */
@property (nonatomic, strong) UIImageView * locationImageView;
/** 定位显示框 */
@property (nonatomic, strong) UITextField * locationTextField;
/** 定位下拉菜单按钮 */
@property (nonatomic, strong) UIButton * locationButton;
/** 返回按钮 */
@property (nonatomic, strong) UIButton * backButton;
/** 定位(机构信息)列表 */
@property (nonatomic, strong) UITableView * addressTableView;
/** 定位（机构信息） */
@property (nonatomic, strong) NSArray * addressArr;
/** organizationId */
@property (nonatomic, assign) NSInteger organizationId;
/** 卡号图标 */
@property (nonatomic, strong) UIImageView * cardImageView;
/** 卡号框 */
@property (nonatomic, strong) UITextField * cardTextField;
/** 卡密码图标 */
@property (nonatomic, strong) UIImageView * cardPwdImageView;
/** 卡密码框 */
@property (nonatomic, strong) UITextField * cardPwdTextField;
/** 注册方式 */
@property (nonatomic, strong) UIView * registWay;
/** 选择注册方式 */
@property (nonatomic, strong) UIButton * registWayButton;
/** 注册方式选项 */
@property (nonatomic, strong) UIView * optionView;
/** <#注释#> */
@property (nonatomic, strong) UIView * nameView;
/** <#注释#> */
@property (nonatomic, strong) UIView * idView;
/** <#注释#> */
@property (nonatomic, strong) UIView * genderView;
/** <#注释#> */
@property (nonatomic, strong) UIView * registWayID;
/** <#注释#> */
@property (nonatomic, strong) UIView * registWayPhone;
/** <#注释#> */
@property (nonatomic, strong) UIView * registWayCommen;
/** <#注释#> */
@property (nonatomic, strong) UITextField * nameTextField;
/** <#注释#> */
@property (nonatomic, strong) UITextField * idTextField;
/** <#注释#> */
@property (nonatomic, copy) NSString * genderStr;

@end

@implementation RegistViewController

#pragma mark - lazy load

- (UIView *)registWayCommen{
    if (!_registWayCommen) {
        _registWayCommen = [[UIView alloc] initWithFrame:CGRectMake(0, _registWayPhone.bottom, SCREEN_W, HeightScale(200))];
   
        _pwdImageView = [[UIImageView alloc] initWithFrame:CGRectMake(WidthScale(40), HeightScale(20), WidthScale(14), HeightScale(16))];
        _pwdImageView.image = ImageNamed(@"lock-icon");
        [_registWayCommen addSubview:_pwdImageView];
        
        
        
        _pwdTextField = [[UITextField alloc] initWithFrame:CGRectMake(_pwdImageView.right + WidthScale(15), HeightScale(10), SCREEN_W - WidthScale(180), HeightScale(40))];
        _pwdTextField.placeholder = @"输入密码";
        _pwdTextField.font = FONTSIZE(17);
        UIImageView *underLine = [[UIImageView alloc] initWithFrame:CGRectMake(WidthScale(20), _pwdTextField.bottom - 0.5, SCREEN_W - WidthScale(40), 0.5)];
        underLine.image = ImageNamed(@"fengexian");
        _pwdTextField.secureTextEntry = YES;
        _pwdTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        [_registWayCommen addSubview:_pwdTextField];
        [_registWayCommen addSubview:underLine];
        
        
        
        _pwdVisibleButton = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_W - WidthScale(120), _pwdTextField.frame.origin.y, WidthScale(100), HeightScale(40))];
        [_pwdVisibleButton setTitle:@"显示" forState:UIControlStateNormal];
        _pwdVisibleButton.titleLabel.font = FONTSIZE(17);
        [_pwdVisibleButton setTitle:@"隐藏" forState:UIControlStateSelected];
        [_pwdVisibleButton setTitleColor:COLOR_GREEN forState:UIControlStateNormal];
        [_pwdVisibleButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
        __weak typeof(self)weakSelf = self;
        [_pwdVisibleButton initWithBlock:^(UIButton * pwdVisibleButton) {
            pwdVisibleButton.selected = !pwdVisibleButton.selected;
            if (pwdVisibleButton.selected) {
                DLog(@"显示");
                weakSelf.pwdTextField.secureTextEntry = NO;
            } else {
                DLog(@"隐藏");
                weakSelf.pwdTextField.secureTextEntry = YES;
            }
        } for:UIControlEventTouchUpInside];
        [_registWayCommen addSubview:_pwdVisibleButton];
        
        
        
        _correctButton = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_W / 2 - WidthScale(60), self.licenceButton.frame.origin.y + 0.5, WidthScale(44), HeightScale(44))];
        [_correctButton setImage:ImageNamed(@"yonghuxieyiR") forState:UIControlStateNormal];
        [_correctButton setImage:ImageNamed(@"yonghuxieyi") forState:UIControlStateSelected];
        _correctButton.selected = YES;
        [_correctButton initWithBlock:^(UIButton * correctButton) {
            DLog(@"用户协议");
            correctButton.selected = !correctButton.selected;
        } for:UIControlEventTouchUpInside];
        [_registWayCommen addSubview:_correctButton];
        
        
        
        _cardImageView = [[UIImageView alloc] initWithFrame:CGRectMake(_pwdImageView.left - 1, CGRectGetMaxY(_pwdTextField.frame) + HeightScale(21), WidthScale(16), HeightScale(14))];
        _cardImageView.image = ImageNamed(@"卡号");
        [_registWayCommen addSubview:_cardImageView];
        
        
        
        _cardTextField = [[UITextField alloc] initWithFrame:CGRectMake(_pwdTextField.left, CGRectGetMaxY(_pwdTextField.frame) + HeightScale(10), _pwdTextField.width, _pwdTextField.height)];
        _cardTextField.placeholder = @"卡号";
        if (self.cardNumber) {
            _cardTextField.enabled = NO;
        } else {
            _cardTextField.enabled = YES;
            _cardTextField.delegate = self;
            _cardTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        }
        _cardTextField.text = self.cardNumber;
        _cardTextField.font = FONTSIZE(17);
        UIImageView *underLine1 = [[UIImageView alloc] initWithFrame:CGRectMake(WidthScale(20), _cardTextField.bottom - 0.5, SCREEN_W - WidthScale(40), 0.5)];
        underLine1.image = ImageNamed(@"fengexian");
        [_registWayCommen addSubview:_cardTextField];
        [_registWayCommen addSubview:underLine1];
        
        
        
        _cardPwdImageView = [[UIImageView alloc] initWithFrame:CGRectMake(_pwdImageView.left, CGRectGetMaxY(_cardTextField.frame) + HeightScale(20), WidthScale(14), HeightScale(16))];
        _cardPwdImageView.image = ImageNamed(@"lock-icon");
        [_registWayCommen addSubview:_cardPwdImageView];
        
        
        
        _cardPwdTextField = [[UITextField alloc] initWithFrame:CGRectMake(_pwdTextField.left, CGRectGetMaxY(_cardTextField.frame) + HeightScale(10), _pwdTextField.width, _pwdTextField.height)];
        if (self.cardPwd) {
            _cardPwdTextField.enabled = NO;
        } else {
            _cardPwdTextField.enabled = YES;
            _cardPwdTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        }
        _cardPwdTextField.placeholder = @"卡密码";
        _cardPwdTextField.text = self.cardPwd;
        _cardPwdTextField.font = FONTSIZE(17);
        UIImageView *underLine2 = [[UIImageView alloc] initWithFrame:CGRectMake(WidthScale(20), _cardPwdTextField.bottom - 0.5, SCREEN_W - WidthScale(40), 0.5)];
        underLine2.image = ImageNamed(@"fengexian");
        [_registWayCommen addSubview:_cardPwdTextField];
        [_registWayCommen addSubview:underLine2];
        
        
        
        _locationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(_pwdImageView.left, CGRectGetMaxY(_cardPwdTextField.frame) + HeightScale(20), WidthScale(14), HeightScale(16))];
        _locationImageView.image = ImageNamed(@"dingwei");
        [_registWayCommen addSubview:_locationImageView];
        
        
        
        _locationTextField = [[UITextField alloc] initWithFrame:CGRectMake(_pwdTextField.left, CGRectGetMaxY(_cardPwdTextField.frame) + HeightScale(10), _pwdTextField.width, _pwdTextField.height)];
        _locationTextField.placeholder = @"定位";
        _locationTextField.enabled = NO;
        _locationTextField.text = self.location;
        _locationTextField.font = FONTSIZE(17);
        UIImageView *underLine3 = [[UIImageView alloc] initWithFrame:CGRectMake(WidthScale(20), _locationTextField.bottom - 0.5, SCREEN_W - WidthScale(40), 0.5)];
        underLine3.image = ImageNamed(@"fengexian");
        [_registWayCommen addSubview:_locationTextField];
        [_registWayCommen addSubview:underLine3];
        
        
        
        
        _locationButton = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_W - WidthScale(120), _locationTextField.frame.origin.y, WidthScale(100), HeightScale(40))];
        [_locationButton setTitle:@"所属机构" forState:UIControlStateNormal];
        [_locationButton setTitleColor:COLOR_FONTGRAY forState:UIControlStateNormal];
        _locationButton.titleLabel.font = FONTSIZE(16);
        //        _locationButton.imageEdgeInsets = UIEdgeInsetsMake(0, -2, 0, +2);
        //        [_locationButton setImage:ImageNamed(@"sanjiao") forState:UIControlStateNormal];
        [_locationButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];

        [_locationButton initWithBlock:^(UIButton * locationButton) {
            DLog(@"所属机构");
            if (locationButton.selected) {
                [weakSelf.addressTableView removeFromSuperview];
                locationButton.selected = NO;
            } else {
                //                [weakSelf requestOrganizationList];
            }
            
            
        } for:UIControlEventTouchUpInside];
        [_registWayCommen addSubview:_locationButton];
        
    }
    return _registWayCommen;
}

- (UIView *)registWayPhone{
    if (!_registWayPhone) {
        _registWayPhone = [[UIView alloc] initWithFrame:CGRectMake(0, self.registWay.bottom, SCREEN_W, HeightScale(100))];
        
        
        _phoneImageView = [[UIImageView alloc] initWithFrame:CGRectMake(WidthScale(40), HeightScale(20), WidthScale(11.5), HeightScale(18))];
        _phoneImageView.image = ImageNamed(@"number");
        [_registWayPhone addSubview:_phoneImageView];
        
        
        
        _phoneTextField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.phoneImageView.frame) + WidthScale(15), HeightScale(10), SCREEN_W - WidthScale(180), HeightScale(40))];
        _phoneTextField.placeholder = @"输入手机号";
        _phoneTextField.font = FONTSIZE(17);
        
        UIImageView *underLine = [[UIImageView alloc] initWithFrame:CGRectMake(WidthScale(20), _phoneTextField.bottom - 0.5, SCREEN_W - WidthScale(40), 0.5)];
        underLine.image = ImageNamed(@"fengexian");
        _phoneTextField.keyboardType = UIKeyboardTypeNumberPad;
        _phoneTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        [_registWayPhone addSubview:_phoneTextField];
        [_registWayPhone addSubview:underLine];
        
        
        
        
        _idCodeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.phoneImageView.frame.origin.x, CGRectGetMaxY(self.phoneTextField.frame) + HeightScale(20), WidthScale(14), HeightScale(16))];
        _idCodeImageView.image = ImageNamed(@"yanzhengma");
        [_registWayPhone addSubview:_idCodeImageView];
        
        
        
        _idCodeTextField = [[UITextField alloc] initWithFrame:CGRectMake(self.phoneTextField.frame.origin.x, CGRectGetMaxY(self.phoneTextField.frame) + HeightScale(10), self.phoneTextField.frame.size.width, self.phoneTextField.frame.size.height)];
        _idCodeTextField.placeholder = @"输入验证码";
        _idCodeTextField.font = FONTSIZE(17);
        _idCodeTextField.keyboardType = UIKeyboardTypeNumberPad;
        UIImageView *underLine1 = [[UIImageView alloc] initWithFrame:CGRectMake(WidthScale(20), _idCodeTextField.bottom - 0.5, SCREEN_W - WidthScale(40), 0.5)];
        underLine1.image = ImageNamed(@"fengexian");
        _idCodeTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        [_registWayPhone addSubview:self.idCodeTextField];
        [_registWayPhone addSubview:underLine1];
        
        
        
        _idCodeButton = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_W - WidthScale(120), self.idCodeTextField.frame.origin.y, WidthScale(100), HeightScale(40))];
        [_idCodeButton setTitle:@"获取" forState:UIControlStateNormal];
        _idCodeButton.titleLabel.font = FONTSIZE(17);
        [_idCodeButton setTitleColor:COLOR_GREEN forState:UIControlStateNormal];
        
        [_idCodeButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
        __weak typeof(self) weakSelf = self;
        [_idCodeButton initWithBlock:^(UIButton * idCodeButton) {
            
            if (idCodeButton.enabled) {
                DLog(@"获取验证码");
                [weakSelf requestIDCode];
                
            }
        } for:UIControlEventTouchUpInside];

        [_registWayPhone addSubview:_idCodeButton];
        
    }
    return _registWayPhone;
}

- (UIView *)registWayID{
    if (!_registWayID) {
        _registWayID = [[UIView alloc] initWithFrame:CGRectMake(0, self.registWay.bottom, SCREEN_W, HeightScale(150))];
        
        _nameView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, HeightScale(50))];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(WidthScale(40), HeightScale(23.5), WidthScale(13.5), HeightScale(13))];
        imageView.image = [UIImage imageNamed:@"name"];
        [_nameView addSubview:imageView];
        
        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(imageView.right + WidthScale(15), HeightScale(10), SCREEN_W - WidthScale(180), HeightScale(40))];
        self.nameTextField = textField;
        textField.placeholder = @"输入真实姓名";
        textField.font = FONTSIZE(17);
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        [_nameView addSubview:textField];
        [_registWayID addSubview:_nameView];
        UIImageView *underline = [[UIImageView alloc] initWithFrame:CGRectMake(WidthScale(20), _nameView.bottom - 0.5, SCREEN_W - WidthScale(40), 0.5)];
        underline.image = [UIImage imageNamed:@"fengexian"];
        [_nameView addSubview:underline];
        
        
        
        _idView = [[UIView alloc] initWithFrame:CGRectMake(0, _nameView.bottom, SCREEN_W, HeightScale(50))];
        UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(imageView.left, HeightScale(23), WidthScale(16), HeightScale(14))];
        imageView2.image = [UIImage imageNamed:@"card"];
        [_idView addSubview:imageView2];
        
        UITextField *textField2 = [[UITextField alloc] initWithFrame:CGRectMake(textField.left, HeightScale(10), textField.width, textField.height)];
        self.idTextField = textField2;
        textField2.placeholder = @"输入身份证号";
        textField2.font = FONTSIZE(17);
        textField2.clearButtonMode = UITextFieldViewModeWhileEditing;
        [_idView addSubview:textField2];
        [_registWayID addSubview:_idView];
        UIImageView *underline1 = [[UIImageView alloc] initWithFrame:CGRectMake(WidthScale(20), _idView.height - 0.5, SCREEN_W - WidthScale(40), 0.5)];
        underline1.image = [UIImage imageNamed:@"fengexian"];
        [_idView addSubview:underline1];
        
        
        
        _genderView = [[UIView alloc] initWithFrame:CGRectMake(0, _idView.bottom, SCREEN_W, HeightScale(50))];
        UIImageView *imageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(imageView.left, HeightScale(23.5), WidthScale(14.5), HeightScale(13))];
        imageView3.image = [UIImage imageNamed:@"xingbie"];
        [_genderView addSubview:imageView3];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(textField.left, HeightScale(10), WidthScale(60), HeightScale(40))];
        label.text = @"性别";
        label.font = FONTSIZE(17);
        [_genderView addSubview:label];
        
        NSArray *arr = @[@"男", @"女"];
        for (int i = 0; i < 2; i++) {
            UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(label.right + i * WidthScale(80), label.top, WidthScale(80), label.height)];
            [_genderView addSubview:button];
            [button setImage:[UIImage imageNamed:@"xingbie_unselected"] forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:@"done"] forState:UIControlStateSelected];
            [button setTitle:arr[i] forState:UIControlStateNormal];
            [button setTitleColor:COLOR_BLACK forState:UIControlStateNormal];
            button.titleLabel.font = FONTSIZE(17);
            button.imageEdgeInsets = UIEdgeInsetsMake(0, -WidthScale(3), 0, WidthScale(3));
            button.titleEdgeInsets = UIEdgeInsetsMake(0, WidthScale(3), 0, -WidthScale(3));
            __weak typeof(self)weakSelf = self;
            [button initWithBlock:^(UIButton *btn) {
                for (UIView *view in _genderView.subviews) {
                    if ([view isKindOfClass:[UIButton class]]) {
                        UIButton *button = (UIButton *)view;
                        button.selected = NO;
                    }
                }
                btn.selected = YES;
                if ([btn.titleLabel.text isEqualToString:@"男"]) {
                    weakSelf.genderStr = @"1";
                } else {
                    weakSelf.genderStr = @"2";
                }
                
            } for:UIControlEventTouchUpInside];
        }
        [_registWayID addSubview:_genderView];
        UIImageView *underline2 = [[UIImageView alloc] initWithFrame:CGRectMake(WidthScale(20), _genderView.height - 0.5, SCREEN_W - WidthScale(40), 0.5)];
        underline2.image = [UIImage imageNamed:@"fengexian"];
        [_genderView addSubview:underline2];
    }
    return _registWayID;
}

- (UIView *)optionView{
    if (!_optionView) {
        _optionView = [[UIView alloc] initWithFrame:[AppDelegate shareDelegate].window.bounds];
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(self.registWayButton.left, self.registWay.bottom - HeightScale(3), self.registWayButton.width, HeightScale(80))];
        view.backgroundColor = COLOR_DEEPBGCOLOR;
        [_optionView addSubview:view];
        __weak typeof(self)weakSelf = self;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
            [weakSelf.optionView removeFromSuperview];
        }];
        [_optionView addGestureRecognizer:tap];
        
        NSArray *arr = @[@"手机号", @"身份证号"];
        for (int i = 0; i < 2; i++) {
            UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, i * HeightScale(40), view.width, HeightScale(40))];
            [view addSubview:button];
            [button setTitle:arr[i] forState:UIControlStateNormal];
            [button setTitleColor:COLOR_BLACK forState:UIControlStateNormal];
            button.titleLabel.font = FONTSIZE(16);
            [button initWithBlock:^(UIButton *btn) {
                [weakSelf.registWayButton setTitle:btn.titleLabel.text forState:UIControlStateNormal];
                [weakSelf.optionView removeFromSuperview];
                
                [weakSelf.registWayID removeFromSuperview];
                [weakSelf.registWayPhone removeFromSuperview];
                
                if ([btn.titleLabel.text isEqualToString:@"手机号"]) {
                    [weakSelf.view addSubview:weakSelf.registWayPhone];
                    weakSelf.registWayCommen.top = weakSelf.registWayPhone.bottom;
                } else {
                    [weakSelf.view addSubview:weakSelf.registWayID];
                    weakSelf.registWayCommen.top = weakSelf.registWayID.bottom;
                }
                
            } for:UIControlEventTouchUpInside];
        }
    }
    return _optionView;
}

- (UIView *)registWay{
    if (!_registWay) {
        _registWay = [[UIView alloc] initWithFrame:CGRectMake(0, self.headerImageView.bottom + HeightScale(10), SCREEN_W, HeightScale(40))];
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(WidthScale(40), HeightScale(12), WidthScale(14), HeightScale(16))];
        imageView.image = [UIImage imageNamed:@"registerway"];
        [_registWay addSubview:imageView];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(imageView.right + WidthScale(15), 0, WidthScale(80), _registWay.height)];
        label.text = @"注册方式";
        label.font = FONTSIZE(17);
        [_registWay addSubview:label];
        
        
        _registWayButton = [[UIButton alloc] initWithFrame:CGRectMake(label.right, 0, WidthScale(120), _registWay.height)];
        [_registWayButton setTitle:@"手机号" forState:UIControlStateNormal];
        [_registWayButton setTitleColor:COLOR_BLACK forState:UIControlStateNormal];
        _registWayButton.titleLabel.font = FONTSIZE(16);
        [_registWay addSubview:_registWayButton];
        
        UIImageView *sanjiao = [[UIImageView alloc] initWithFrame:CGRectMake(_registWayButton.width - WidthScale(10), (_registWayButton.height - HeightScale(4)) / 2, WidthScale(7), HeightScale(4))];
        sanjiao.image = [UIImage imageNamed:@"sanjiao"];
        sanjiao.userInteractionEnabled = YES;
        [_registWayButton addSubview:sanjiao];
        __weak typeof(self)weakSelf = self;
        [_registWayButton initWithBlock:^(id obj) {
            
            [weakSelf.view addSubview:weakSelf.optionView];
            
        } for:UIControlEventTouchUpInside];
        
        
        UIImageView *underLine = [[UIImageView alloc] initWithFrame:CGRectMake(WidthScale(20), _registWay.height - 0.5, SCREEN_W - WidthScale(40), 0.5)];
        underLine.image = ImageNamed(@"fengexian");
        [_registWay addSubview:underLine];
    }
    return _registWay;
}

- (UITableView *)addressTableView{
    if (!_addressTableView) {
        
        _addressTableView = [[UITableView alloc] initWithFrame:CGRectMake(WidthScale(80), CGRectGetMaxY(self.locationButton.frame) + HeightScale(0.5), SCREEN_W - WidthScale(160), HeightScale(144)) style:UITableViewStylePlain];
        _addressTableView.delegate = self;
        _addressTableView.dataSource = self;
        _addressTableView.bounces = NO;
        _addressTableView.backgroundColor = COLOR_DEEPBGCOLOR;
        _addressTableView.rowHeight = HeightScale(36);
        _addressTableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    }
    return _addressTableView;
}

- (UIButton *)backButton{
    if (!_backButton) {
        _backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 20, 44, 44)];
        [_backButton setImage:ImageNamed(@"back") forState:UIControlStateNormal];
        _backButton.transform = CGAffineTransformMakeRotation(-M_PI_2);
        __weak typeof(self)weakSelf = self;
        [_backButton initWithBlock:^(id obj) {
            DLog(@"返回");
            [weakSelf dismissViewControllerAnimated:YES completion:nil];
        } for:UIControlEventTouchUpInside];
    }
    return _backButton;
}

- (UIButton *)licenceButton{
    if (!_licenceButton) {
        _licenceButton = [[UIButton alloc] initWithFrame:CGRectMake((SCREEN_W - WidthScale(70)) / 2 + WidthScale(10), SCREEN_H - HeightScale(50), WidthScale(70), HeightScale(44))];
        [_licenceButton setTitle:@"用户协议" forState:UIControlStateNormal];
        _licenceButton.selected = YES;
        [_licenceButton setTitleColor:COLOR_FONTLIGHTGRAY forState:UIControlStateNormal];
        _licenceButton.titleLabel.font = FONTSIZE(15);
        [_licenceButton initWithBlock:^(UIButton * licenceButton) {
            DLog(@"查看用户协议");
        } for:UIControlEventTouchUpInside];
    }
    return _licenceButton;
}

- (UIButton *)idCodeButton{
    if (!_idCodeButton) {
    }
    return _idCodeButton;
}

/** NSTimer selector */
- (void)timerCountdown:(NSTimer *)timer{
    if (self.seconds) {
        [self.idCodeButton setTitle:[NSString stringWithFormat:@"已发送(%lds)", (long)self.seconds] forState:UIControlStateDisabled];
        self.seconds -= 1;
        DLog(@"倒计时 %ld", self.seconds);
    } else {
        [self.idCodeButton setTitle:[NSString stringWithFormat:@"已发送"] forState:UIControlStateDisabled];
        self.idCodeButton.enabled = YES;
        [self.timer invalidate];
        self.timer = nil;
        
    }
    
}

- (UIButton *)registButton{
    if (!_registButton) {
        _registButton = [[UIButton alloc] initWithFrame:CGRectMake((SCREEN_W - WidthScale(260)) / 2, SCREEN_H - HeightScale(110), WidthScale(260), HeightScale(61))];
        [_registButton setImage:ImageNamed(@"zhuce-s") forState:UIControlStateNormal];
        __weak typeof(self)weakSelf = self;
        [_registButton initWithBlock:^(id obj) {
            DLog(@"注册");
            if ([_registWayButton.titleLabel.text isEqualToString:@"手机号"]) {
                [weakSelf requestRegistPhone];
            } else {
                [weakSelf requestRegistID];
            }
            
        } for:UIControlEventTouchUpInside];
    }
    return _registButton;
}

- (UIImageView *)headerImageView{
    if (!_headerImageView) {
        _headerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_W / 2)];
        _headerImageView.image = ImageNamed(@"background");
        _headerImageView.userInteractionEnabled = YES;
    }
    return _headerImageView;
}

#pragma mark - life cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = COLOR_LIGHTBGCOLOR;
    
    [self.view addSubview:self.headerImageView];
    [self.view addSubview:self.backButton];
    [self.view addSubview:self.registWay];
    
    [self.view addSubview:self.registWayPhone];
    [self.view addSubview:self.registWayCommen];
    
    [self.view addSubview:self.registButton];
    [self.view addSubview:self.licenceButton];
    
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    if (_timer) {
        [_timer invalidate];
        _timer = nil;
    }
    
}

- (void)dealloc{
    DLog(@"%s", __func__);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource Methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.addressArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.textLabel.text = self.addressArr[indexPath.row][@"name"];
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    cell.textLabel.font = FONTSIZE(15);
    cell.textLabel.textColor = COLOR_FONTGRAY;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = COLOR_DEEPBGCOLOR;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    _locationTextField.text = self.addressArr[indexPath.row][@"name"];
    self.organizationId = [self.addressArr[indexPath.row][@"id"] integerValue];
    [self.addressTableView removeFromSuperview];
}

#pragma mark - data request

/** 获取短信验证码 */
- (void)requestIDCode{
    if ([Tool isMobileNumber:self.phoneTextField.text]) {
        
        NSDictionary *parameters = @{@"userCode": self.phoneTextField.text};
        NSString *URL = [NSString stringWithFormat:KIDCodeURL, KHostAddress, self.phoneTextField.text];
        [ZRRequestManager POST:URL parameters:parameters success:^(id responseObject) {
            DLog(@"%@", responseObject);
            if ([[responseObject objectForKey:@"error"] integerValue] == 0) {
                [ZRProgressHUD showMsgWithoutView:@"验证码已发送到您的手机"];
                
                self.seconds = 60;
                self.idCodeButton.enabled = NO;
                self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerCountdown:) userInfo:nil repeats:YES];
                
                [_idCodeButton setTitle:@"已发送" forState:UIControlStateDisabled];
                [_idCodeButton setTitleColor:COLOR_FONTLIGHTGRAY forState:UIControlStateDisabled];
            } else {
                [ZRProgressHUD showMsgWithoutView:[responseObject objectForKey:@"data"]];
            }
        } failure:^(NSError *error) {
            DLog(@"获取验证码失败");
        }];
    } else {
        [ZRProgressHUD showMsgWithoutView:@"请输入正确的手机号码"];
    }
}

/** 注册 */
- (void)requestRegistPhone{
    
    if (self.phoneTextField.text.length == 0) {
        [ZRProgressHUD showMsgWithoutView:@"您还没有填写手机号码"];
        return;
    }
    
    if (self.phoneTextField.text.length != 11) {
        [ZRProgressHUD showMsgWithoutView:@"请输入正确的手机号码"];
        return;
    }
    
    if (self.idCodeTextField.text.length == 0) {
        [ZRProgressHUD showMsgWithoutView:@"您还没有输入验证码"];
        return;
    }
    
    if (self.pwdTextField.text.length == 0) {
        [ZRProgressHUD showMsgWithoutView:@"您还没有输入密码"];
        return;
    }
    
    if (self.cardTextField.text.length != 10) {
        [ZRProgressHUD showMsgWithoutView:@"请检查您的卡号是否正确"];
        return;
    }
    
    if (self.locationTextField.text.length == 0) {
        [ZRProgressHUD showMsgWithoutView:@"请检查您的卡号是否正确"];
        return;
    }
    
    if (self.cardPwdTextField.text.length == 0) {
        [ZRProgressHUD showMsgWithoutView:@"您还没有输入卡密码"];
        return;
    }
    
    NSString *URL = [NSString stringWithFormat:KRegistURL, KHostAddress, self.phoneTextField.text, self.pwdTextField.text, self.idCodeTextField.text, self.cardNumber ? self.cardNumber : self.cardTextField.text, self.cardPwd ? self.cardPwd : self.cardPwdTextField.text];
    [ZRRequestManager POST:URL parameters:nil success:^(id responseObject) {
        DLog(@"%@", responseObject);
        if ([[responseObject objectForKey:@"error"] integerValue] == 0) {
            [ZRProgressHUD showMsgWithoutView:@"注册成功"];
            
            [self login];
            
        } else {
            [ZRProgressHUD showMsgWithoutView:[responseObject objectForKey:@"data"]];
        }
    } failure:^(NSError *error) {
        DLog(@"注册失败");
        
    }];
}

- (void)requestRegistID{
    if (self.nameTextField.text.length == 0) {
        [ZRProgressHUD showMsgWithoutView:@"请输入真实姓名"];
        return;
    }
    
    if (![Tool isValidateIDNumber:self.idTextField.text]) {
        [ZRProgressHUD showMsgWithoutView:@"非法的身份证号，请重新输入"];
        return;
    }
    
    if (self.genderStr.length == 0) {
        [ZRProgressHUD showMsgWithoutView:@"请选择性别"];
        return;
    }
    if (self.pwdTextField.text.length == 0) {
        [ZRProgressHUD showMsgWithoutView:@"您还没有输入密码"];
        return;
    }
    
    if (self.cardTextField.text.length != 10) {
        [ZRProgressHUD showMsgWithoutView:@"请检查您的卡号是否正确"];
        return;
    }
    
    if (self.locationTextField.text.length == 0) {
        [ZRProgressHUD showMsgWithoutView:@"请检查您的卡号是否正确"];
        return;
    }
    
    if (self.cardPwdTextField.text.length == 0) {
        [ZRProgressHUD showMsgWithoutView:@"您还没有输入卡密码"];
        return;
    }
    
    NSString *url = [NSString stringWithFormat:KRegistWithID, KHostAddress, self.pwdTextField.text, self.cardNumber ? self.cardNumber : self.cardTextField.text, self.cardPwd ? self.cardPwd : self.cardPwdTextField.text, self.idTextField.text, self.nameTextField.text, _genderStr];
    [ZRRequestManager POST:url parameters:nil success:^(id responseObject) {
        DLog(@"%@", responseObject);
        if ([[responseObject objectForKey:@"error"] integerValue] == 0) {
            [ZRProgressHUD showMsgWithoutView:@"注册成功"];
            
            [self login];
            
        } else {
            [ZRProgressHUD showMsgWithoutView:[responseObject objectForKey:@"data"]];
        }

    } failure:^(NSError *error) {
        DLog(@"%@", error);
    }];

}

#pragma mark - 获取读者卡信息
- (void)getCardInfo{
    if (self.cardTextField.text.length == 10) {
        NSString *URL = [NSString stringWithFormat:KcardInfo, KHostAddress, self.cardTextField.text];
        [ZRRequestManager POST:URL parameters:nil success:^(id responseObject) {
            if ([[responseObject objectForKey:@"error"] intValue] == 0) {
                self.locationTextField.text = [responseObject objectForKey:@"data"][@"organizationName"];
            } else {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [ZRProgressHUD showMsgWithoutView:@"卡号不存在"];
                    _locationTextField.text = @"";
                });
                
            }
            
        } failure:^(NSError *error) {
            DLog(@"%@", error);
        }];
    } else {
        self.locationTextField.text = @"";
    }
    
}

#pragma mark - UITextFieldDelegate Method
- (void)textFieldDidEndEditing:(UITextField *)textField{
    [self getCardInfo];
}

//注册后自动登录
- (void)login{
    
    NSString *URL;
    
    if ([self.registWayButton.titleLabel.text isEqualToString:@"手机号"]) {
        URL = [NSString stringWithFormat:KLoginURL, KHostAddress, self.phoneTextField.text, self.pwdTextField.text];
    } else {
        URL = [NSString stringWithFormat:KLoginURL, KHostAddress, self.idTextField.text, self.pwdTextField.text];
    }
    
    [ZRRequestManager POST:URL parameters:nil success:^(id responseObject) {
        
        DLog(@"%@", responseObject);
        
        if ([[responseObject objectForKey:@"error"] integerValue] == 0) {
            
            [ZRProgressHUD showMsgWithoutView:@"已为您自动登录"];
            
            self.pwdTextField.text = @"";
            
            [AppDelegate shareDelegate].userModel = [UserModel modelWithDictionary:[responseObject objectForKey:@"data"]];
            
            //字典序列化并存储到NSUserDefaults
            NSData *userData = [NSKeyedArchiver archivedDataWithRootObject:[responseObject objectForKey:@"data"]];
            
            [[NSUserDefaults standardUserDefaults] setObject:userData forKey:@"user"];
            
            [[NSUserDefaults standardUserDefaults] synchronize];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"loginSuccess" object:nil];
            
            [self dismissViewControllerAnimated:YES completion:nil];
            
            
            
        } else {
            
            [ZRProgressHUD showMsgWithoutView:@"自动登录失败"];
            
        }
        
    } failure:^(NSError *error) {
        
        DLog(@"自动登录失败 %@", error);
        
    }];
}

//
///** 获取机构列表 */
//- (void)requestOrganizationList{
//    NSString *URL = [NSString stringWithFormat:KOrganizationList, KHostAddress];
//    [ZRRequestManager POST:URL parameters:nil success:^(id responseObject) {
//        DLog(@"%@", responseObject);
//        if ([[responseObject objectForKey:@"error"] integerValue] == 0) {
//            self.addressArr = [NSArray arrayWithArray:responseObject[@"data"]];
//            
//            [self.view addSubview:self.addressTableView];
//            
//            self.locationButton.selected = YES;
//        }
//        
//    } failure:^(NSError *error) {
//        DLog(@"%@", error);
//    }];
//}

@end
